// 高阶函数作为参数

const arr1 = [1, 2, 3, 4];

function forEach(array, fn) {
    for (let i = 0; i < array.length; i++) {
        fn(array[i], i , array);
    }
}

function filter(array, fn) {
    const result = [];
    for (let i = 0; i < array.length; i++) {
        if (fn(array[i])) {
            result.push(array[i]);
        }
    }
    return result;
}

function map(array, fn) {
    const result = [];
    for (let i = 0; i < array.length; i++) {
        result.push(fn(array[i]));
    }
    return result;
}

function every(array, fn) {
    let result = true;
    for (let value of array) {
        if (!fn(value)) {
            result = false;
            break;
        }
    }
    return result;
}

function some(array, fn) {
    let result = false;
    for (let value of array) {
        if (fn(value)) {
            result = true;
            break;
        }
    }
    return result;
}

function reduce(array, fn) {
    let result = 0;
    for (let value of array) {
        result = fn(result, value);
    }
    
    return result;
}

let a = reduce(arr1, (prev, curent) => {
    return prev + curent;
}) 
console.log(a);
